//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>
package org.netbeans.lib.awtextra;

import java.awt.*;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Hashtable;

// Referenced classes of package org.netbeans.lib.awtextra:
//            AbsoluteConstraints
public class AbsoluteLayout
        implements LayoutManager2, Serializable {

    public AbsoluteLayout() {
        constraints = new Hashtable();
    }

    public void addLayoutComponent(String name, Component comp) {
        throw new IllegalArgumentException();
    }

    public void removeLayoutComponent(Component comp) {
        constraints.remove(comp);
    }

    public Dimension preferredLayoutSize(Container parent) {
        int maxWidth = 0;
        int maxHeight = 0;
        for (Enumeration e = constraints.keys(); e.hasMoreElements();) {
            Component comp = (Component) e.nextElement();
            AbsoluteConstraints ac = (AbsoluteConstraints) constraints.get(comp);
            Dimension size = comp.getPreferredSize();
            int width = ac.getWidth();
            if (width == -1) {
                width = size.width;
            }
            int height = ac.getHeight();
            if (height == -1) {
                height = size.height;
            }
            if (ac.x + width > maxWidth) {
                maxWidth = ac.x + width;
            }
            if (ac.y + height > maxHeight) {
                maxHeight = ac.y + height;
            }
        }

        return new Dimension(maxWidth, maxHeight);
    }

    public Dimension minimumLayoutSize(Container parent) {
        int maxWidth = 0;
        int maxHeight = 0;
        for (Enumeration e = constraints.keys(); e.hasMoreElements();) {
            Component comp = (Component) e.nextElement();
            AbsoluteConstraints ac = (AbsoluteConstraints) constraints.get(comp);
            Dimension size = comp.getMinimumSize();
            int width = ac.getWidth();
            if (width == -1) {
                width = size.width;
            }
            int height = ac.getHeight();
            if (height == -1) {
                height = size.height;
            }
            if (ac.x + width > maxWidth) {
                maxWidth = ac.x + width;
            }
            if (ac.y + height > maxHeight) {
                maxHeight = ac.y + height;
            }
        }

        return new Dimension(maxWidth, maxHeight);
    }

    public void layoutContainer(Container parent) {
        Component comp;
        AbsoluteConstraints ac;
        int width;
        int height;
        for (Enumeration e = constraints.keys(); e.hasMoreElements(); comp.setBounds(ac.x, ac.y, width, height)) {
            comp = (Component) e.nextElement();
            ac = (AbsoluteConstraints) constraints.get(comp);
            Dimension size = comp.getPreferredSize();
            width = ac.getWidth();
            if (width == -1) {
                width = size.width;
            }
            height = ac.getHeight();
            if (height == -1) {
                height = size.height;
            }
        }

    }

    public void addLayoutComponent(Component comp, Object constr) {
        if (!(constr instanceof AbsoluteConstraints)) {
            throw new IllegalArgumentException();
        } else {
            constraints.put(comp, constr);
            return;
        }
    }

    public Dimension maximumLayoutSize(Container target) {
        return new Dimension(0x7fffffff, 0x7fffffff);
    }

    public float getLayoutAlignmentX(Container target) {
        return 0.0F;
    }

    public float getLayoutAlignmentY(Container target) {
        return 0.0F;
    }

    public void invalidateLayout(Container container) {
    }
    static final long serialVersionUID = 0xe55b4b7098765c98L;
    protected Hashtable constraints;
}
